package com.geeyuu.android.activity;

import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.geeyuu.android.GeePrefrence;
import com.geeyuu.android.R;
import com.geeyuu.android.databases.DataBaseHelper;
import com.geeyuu.android.utils.LogUtil;
import com.umeng.analytics.MobclickAgent;

public class MainTabActivity extends ActivityGroup implements OnClickListener {

    private static final String TAG = "MainTabActivity";
    
    private LinearLayout container = null;
    /**
     * 当前tab button id
     */
    private int currentTabResId = R.id.tab_button0;
    private int currentTabIndex = 0;

    private Intent intent = null;
    private static final String[] MODULE_NAMES = { "Module0", "Module1", "Module2", "Module3", "Module4" };

    private Button container0;
    private Button container1;
    private Button container2;
    private Button container3;
    private Button container4;

    private ImageView currentTabLine;
    
    public static boolean isAppShowing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_tab);
        
        isAppShowing = true;

        GeePrefrence.setIsUserRegisted(getApplicationContext(), true);
        
        LogUtil.v(TAG, "onCreate");
        
        container = (LinearLayout) findViewById(R.id.containerBody);
        intent = new Intent(MainTabActivity.this, Activity0.class);
        addContainerView(MODULE_NAMES[0], intent);

        // 模块0
        container0 = (Button) findViewById(R.id.tab_button0);
        container0.setOnClickListener(this);

        // 模块1
        container1 = (Button) findViewById(R.id.tab_button1);
        container1.setOnClickListener(this);

        // 模块2
        container2 = (Button) findViewById(R.id.tab_button2);
        container2.setOnClickListener(this);

        // 模块3
        container3 = (Button) findViewById(R.id.tab_button3);
        container3.setOnClickListener(this);

        // 模块4
        container4 = (Button) findViewById(R.id.tab_button4);
        container4.setOnClickListener(this);

        initCurrentTabLine();

    }

    private int distanceOne;
    private int distanceTwo;
    private int distanceThree;
    private int distanceFour;
    private int offset;

    /**
     * 
     * 初始化动画
     */
    private void initCurrentTabLine() {
        currentTabLine = (ImageView) findViewById(R.id.current_tab_line);
        int bmpWidth = BitmapFactory.decodeResource(getResources(), R.drawable.current_tab_icon).getWidth();// 获取图片宽度
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int screenWidth = displayMetrics.widthPixels;// 获取分辨率宽度
        offset = (screenWidth / 5 - bmpWidth) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        currentTabLine.setImageMatrix(matrix);// 设置动画初始位置
        distanceOne = offset * 2 + bmpWidth;// 页卡1 -> 页卡2 偏移量
        distanceTwo = distanceOne * 2;
        distanceThree = distanceOne * 3;
        distanceFour = distanceOne * 4;
    }

    private void setTabLineStateOnChangeTab(int lastIndex, int currentIndex) {
        Animation animation = null;
        switch (lastIndex) {
        case 0:
            if (currentIndex == 1) {
                animation = new TranslateAnimation(offset, distanceOne, 0, 0);
            } else if (currentIndex == 2) {
                animation = new TranslateAnimation(offset, distanceTwo, 0, 0);
            } else if (currentIndex == 3) {
                animation = new TranslateAnimation(offset, distanceThree, 0, 0);
            } else if (currentIndex == 4) {
                animation = new TranslateAnimation(offset, distanceFour, 0, 0);
            }
            break;
        case 1:
            if (currentIndex == 0) {
                animation = new TranslateAnimation(distanceOne, offset, 0, 0);
            } else if (currentIndex == 2) {
                animation = new TranslateAnimation(distanceOne, distanceTwo, 0, 0);
            } else if (currentIndex == 3) {
                animation = new TranslateAnimation(distanceOne, distanceThree, 0, 0);
            } else if (currentIndex == 4) {
                animation = new TranslateAnimation(distanceOne, distanceFour, 0, 0);
            }
            break;
        case 2:
            if (currentIndex == 0) {
                animation = new TranslateAnimation(distanceTwo, offset, 0, 0);
            } else if (currentIndex == 1) {
                animation = new TranslateAnimation(distanceTwo, distanceOne, 0, 0);
            } else if (currentIndex == 3) {
                animation = new TranslateAnimation(distanceTwo, distanceThree, 0, 0);
            } else if (currentIndex == 4) {
                animation = new TranslateAnimation(distanceTwo, distanceFour, 0, 0);
            }
            break;
        case 3:
            if (currentIndex == 0) {
                animation = new TranslateAnimation(distanceThree, offset, 0, 0);
            } else if (currentIndex == 1) {
                animation = new TranslateAnimation(distanceThree, distanceOne, 0, 0);
            } else if (currentIndex == 2) {
                animation = new TranslateAnimation(distanceThree, distanceTwo, 0, 0);
            } else if (currentIndex == 4) {
                animation = new TranslateAnimation(distanceThree, distanceFour, 0, 0);
            }
            break;
        case 4:
            if (currentIndex == 0) {
                animation = new TranslateAnimation(distanceFour, offset, 0, 0);
            } else if (currentIndex == 1) {
                animation = new TranslateAnimation(distanceFour, distanceOne, 0, 0);
            } else if (currentIndex == 2) {
                animation = new TranslateAnimation(distanceFour, distanceTwo, 0, 0);
            } else if (currentIndex == 3) {
                animation = new TranslateAnimation(distanceFour, distanceThree, 0, 0);
            }
            break;
        }
        animation.setFillAfter(true);// True:图片停在动画结束位置
        animation.setDuration(200);
        currentTabLine.startAnimation(animation);
    }

    @Override
    public void onClick(View v) {
        if (currentTabResId == v.getId()) {
            return;
        }
        currentTabResId = v.getId();
        container.removeAllViews();
        switch (currentTabResId) {
        case R.id.tab_button0:
            intent.setClass(MainTabActivity.this, Activity0.class);
            addContainerView(MODULE_NAMES[0], intent);
            setTabLineStateOnChangeTab(currentTabIndex, 0);
            currentTabIndex = 0;
            break;
        case R.id.tab_button1:
            intent.setClass(MainTabActivity.this, Activity1.class);
            addContainerView(MODULE_NAMES[1], intent);
            setTabLineStateOnChangeTab(currentTabIndex, 1);
            currentTabIndex = 1;
            break;
        case R.id.tab_button2:
            intent.setClass(MainTabActivity.this, Activity2.class);
            addContainerView(MODULE_NAMES[2], intent);
            setTabLineStateOnChangeTab(currentTabIndex, 2);
            currentTabIndex = 2;
            break;
        case R.id.tab_button3:
            intent.setClass(MainTabActivity.this, Activity3.class);
            addContainerView(MODULE_NAMES[3], intent);
            setTabLineStateOnChangeTab(currentTabIndex, 3);
            currentTabIndex = 3;
            break;
        case R.id.tab_button4:
            intent.setClass(MainTabActivity.this, Activity4.class);
            addContainerView(MODULE_NAMES[4], intent);
            setTabLineStateOnChangeTab(currentTabIndex, 4);
            currentTabIndex = 4;
            break;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "退出");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
        case 0:
            finish();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void addContainerView(String desc, Intent intent) {
        container.addView(getLocalActivityManager().startActivity(desc, intent).getDecorView());
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        LogUtil.v(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        LogUtil.v(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtil.v(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        isAppShowing = false;
        DataBaseHelper.close();
        LogUtil.v(TAG, "onDestroy");
        super.onDestroy();
    }

}
